/**
 * @description 判读是否为外链
 * @param path
 * @returns {boolean}
 */
export function isExternal(rule, value, callback) {
  if (!/^(https?:|mailto:|tel:)/.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 校验密码是否小于6位
 * @param value
 * @returns {boolean}
 */
export function isPassword(rule, value, callback) {
  if (value.length < 6) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 校验验证码格式
 * @param value
 * @returns {boolean}
 */
export function isCode(rule, value, callback) {
  const reg = /^\d{6}$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 判断是否为数字
 * @param value
 * @returns {boolean}
 */
export function isNumber(rule, value, callback) {
  const reg = /^[0-9]*$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 判断是否为百分数
 * @param value
 * @returns {boolean}
 */
export function isAnyPercentage(rule, value, callback) {
  const reg = /^-?\d+(\.\d+)?%$/
  console.log(value)

  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 判断是否为正确身份证
 * @param value
 * @returns {boolean}
 */
export function isValidIDCard(rule, value, callback) {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 判断是否为非负百分数
 * @param value
 * @returns {boolean}
 */
export function isIntPercentage(rule, value, callback) {
  const reg = /^\d+(\.\d+)?%$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 判断是否为非负数
 * @param value
 * @returns {boolean}
 */
export function isNegNum(rule, value, callback) {
  const reg = /^[+]{0,1}([1-9][0-9]{0,})$|^[+]{0,1}([1-9][0-9]{0,}\.\d+)$|^[+]{0,1}([0]\.(?!0+$)\d{1,})$/
  if (!reg.test(value) && value != 0) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 判断是否非负整数
 * @param value
 * @returns {boolean}
 */
export function isIntNum(rule, value, callback) {
  const reg = /^([1-9]\d*|[0]{1,1})$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 判断是否正整数
 * @param value
 * @returns {boolean}
 */
export function isInt(rule, value, callback) {
  const reg = /^[1-9]+\d*$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 正数、负数、和小数
 * @param value
 * @returns {boolean}
 */
export function isAnyNum(rule, value, callback) {
  const reg = /^(\-|\+)?\d+(\.\d+)?$/
  if (!reg.test(value) & (value != 0)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 正数、负数、和小数(百分号版本)
 * @param value
 * @returns {boolean}
 */
export function isPNum(rule, value, callback) {
  let val = value
  if (value && value.indexOf('%') != -1) {
    val = value.replace(/(^%!)|(%$)/g, '')
  }
  const reg = /^(\-|\+)?\d+(\.\d+)?$/
  if (!reg.test(val) & (val != 0)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
/**
 * @description 判断是否是名称
 * @param value
 * @returns {boolean}
 */
export function isName(rule, value, callback) {
  const reg = /^[\u4e00-\u9fa5a-zA-Z0-9]+$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否为IP
 * @param ip
 * @returns {boolean}
 */
export function isIP(rule, value, callback) {
  const reg =
    /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否是传统网站
 * @param url
 * @returns {boolean}
 */
export function isUrl(rule, value, callback) {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否是小写字母
 * @param value
 * @returns {boolean}
 */
export function isLowerCase(rule, value, callback) {
  const reg = /^[a-z]+$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否是大写字母
 * @param value
 * @returns {boolean}
 */
export function isUpperCase(rule, value, callback) {
  const reg = /^[A-Z]+$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否是大写字母开头
 * @param value
 * @returns {boolean}
 */
export function isAlphabets(rule, value, callback) {
  const reg = /^[A-Za-z]+$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否是字符串
 * @param value
 * @returns {boolean}
 */
export function isString(rule, value, callback) {
  const reg = typeof value === 'string' || value instanceof String
  if (!reg) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否是数组
 * @param arg
 * @returns {arg is any[]|boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * @description 判断是否是端口号
 * @param value
 * @returns {boolean}
 */
export function isPort(rule, value, callback) {
  const reg = /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否是手机号
 * @param value
 * @returns {boolean}
 */
export function isPhone(rule, value, callback) {
  const reg = /^1\d{10}$/
  return reg.test(value)
}

/**
 * @description 判断是否是身份证号(第二代)
 * @param value
 * @returns {boolean}
 */
export function isIdCard(rule, value, callback) {
  const reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否是邮箱
 * @param value
 * @returns {boolean}
 */
export function isEmail(rule, value, callback) {
  const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否中文
 * @param value
 * @returns {boolean}
 */
export function isChina(rule, value, callback) {
  const reg = /^[\u4E00-\u9FA5]{2,4}$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否为空
 * @param value
 * @returns {boolean}
 */
export function isBlank(rule, value, callback) {
  const reg =
    value === null || false || value === '' || value.trim() === '' || value.toLocaleLowerCase().trim() === 'null'
  if (!reg) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否为固话
 * @param value
 * @returns {boolean}
 */
export function isTel(rule, value, callback) {
  const reg = /^(400|800)([0-9\\-]{7,10})|(([0-9]{4}|[0-9]{3})([- ])?)?([0-9]{7,8})(([- 转])*([0-9]{1,4}))?$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否为数字且最多两位小数
 * @param value
 * @returns {boolean}
 */
export function isNum(rule, value, callback) {
  const reg = /^\d+(\.\d{1,2})?$/
  if (!reg.test(value)) {
    return Promise.reject('请输入数字且最多两位小数', reg.test(value))
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断经度 -180.0～+180.0（整数部分为0～180，必须输入1到5位小数）
 * @param value
 * @returns {boolean}
 */
export function isLongitude(rule, value, callback) {
  const reg = /^[-|+]?(0?\d{1,2}\.\d{1,5}|1[0-7]?\d{1}\.\d{1,5}|180\.0{1,5})$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断纬度 -90.0～+90.0（整数部分为0～90，必须输入1到5位小数）
 * @param value
 * @returns {boolean}
 */
export function isLatitude(rule, value, callback) {
  const reg = /^[-|+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})$/
  if (!reg.test(value)) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description rtsp校验，只要有rtsp://
 * @param value
 * @returns {boolean}
 */
export function isRTSP(rule, value, callback) {
  const reg =
    /^rtsp:\/\/([a-z]{0,10}:.{0,10}@)?(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  const reg1 =
    /^rtsp:\/\/([a-z]{0,10}:.{0,10}@)?(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]):[0-9]{1,5}/
  const reg2 =
    /^rtsp:\/\/([a-z]{0,10}:.{0,10}@)?(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\//
  const v = reg.test(value) || reg1.test(value) || reg2.test(value)
  if (!v) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}

/**
 * @description 判断是否为json
 * @param value
 * @returns {boolean}
 */
export function isJson(rule, value, callback) {
  if (typeof value === 'string') {
    try {
      var obj = JSON.parse(value)
      if (typeof obj === 'object' && obj) {
        return Promise.resolve()
      } else {
        return Promise.reject()
      }
    } catch (e) {
      return Promise.reject()
    }
  }
}
/**
 * @description 判断是否为对象且不为空
 * @param value
 * @returns {boolean}
 */
export function isObjectNotEmpty(rule, value, callback) {
  const flag = typeof value === 'object' && value !== null && Object.keys(value).length > 0
  if (!flag) {
    return Promise.reject()
  } else {
    return Promise.resolve()
  }
}
